package com.contexticon.mediaTest;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.AbstractAction;
import javax.swing.Action;
 
public class PlayerFrameExtended {
 
    private final JFrame frame;
    private final JPanel contentPane;
    private final JButton myButton;
 
    private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
    private final Action action = new SwingAction();
    private final Action action_1 = new SwingAction_1();
 
    public static void main(String[] args) {
    	
    	System.setProperty("jna.library.path", "/Applications/VLC.app/Contents/MacOS/lib/");
 
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new PlayerFrameExtended();
            }
        });
    }
 
    public PlayerFrameExtended() {
        contentPane = new JPanel();
        contentPane.setBackground(Color.black);
 
        mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
        

        
        
        myButton = new JButton("Play");
        myButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				start("/Users/dglodzik/Dropbox/German - Dominik/Sozialismus in China/Sozialismus.mp4");
			}
		});
        
 
        frame = new JFrame("Layout Example");
        frame.setAlwaysOnTop(true);
        frame.setLocation(100, 100);
        frame.setSize(600, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(contentPane);
        
        JSlider slider = new JSlider();
        
        JButton btnPause = new JButton("Pause");
        btnPause.setAction(action);
        
        JLabel lblNewLabel = new JLabel("New label");
        
        JButton btnsBack = new JButton("1s back");
        btnsBack.setAction(action_1);
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
        	gl_contentPane.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_contentPane.createSequentialGroup()
        			.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
        				.addComponent(mediaPlayerComponent, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        				.addGroup(gl_contentPane.createSequentialGroup()
        					.addComponent(slider, GroupLayout.DEFAULT_SIZE, 527, Short.MAX_VALUE)
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addComponent(lblNewLabel))
        				.addGroup(gl_contentPane.createSequentialGroup()
        					.addContainerGap()
        					.addComponent(myButton, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addComponent(btnPause)
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addComponent(btnsBack)))
        			.addContainerGap())
        );
        gl_contentPane.setVerticalGroup(
        	gl_contentPane.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_contentPane.createSequentialGroup()
        			.addComponent(mediaPlayerComponent, GroupLayout.PREFERRED_SIZE, 302, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
        				.addGroup(gl_contentPane.createSequentialGroup()
        					.addComponent(slider, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
        						.addComponent(myButton)
        						.addComponent(btnPause)
        						.addComponent(btnsBack)))
        				.addComponent(lblNewLabel))
        			.addContainerGap())
        );
        contentPane.setLayout(gl_contentPane);
 
        frame.setVisible(true);
    }
 
    public long getCurrentTime() {	
    	return mediaPlayerComponent.getMediaPlayer().getTime();
    }
    
    
    public void gotoTime(float time) {
    	long t = (long) time;
    	mediaPlayerComponent.getMediaPlayer().setTime(t);
    }

    private void start(String mrl) {
        mediaPlayerComponent.getMediaPlayer().playMedia(mrl);
    }
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "Pause");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			mediaPlayerComponent.getMediaPlayer().pause();
		}
	}
	private class SwingAction_1 extends AbstractAction {
		public SwingAction_1() {
			putValue(NAME, "Back 1s");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			mediaPlayerComponent.getMediaPlayer().skip(-1000);
		}
	}
}